import {Http} from '../utils/http'

const mixins = require('../common/mixins/model')

class Vote {
    static async getVoteList(type, page, listLength, size = 6, keywords = '') {
        const api = this._getListApiByType(type)
        const res = await Http.request({
            url: `${api}?page=${page}&size=${size}&kw=${keywords}`,
            method: 'GET'
        })
        res.data.hasMore = mixins.judgeHasMore(res.data.total, listLength, size)
        return res.data
    }

    static async onVote(type, id, count) {
        const params = this._getVoteApiByType(type)
        const res = await Http.request({
            url: params.url,
            data: {
                count,
                [params.idType]: id
            }
        })
        return res
    }

    static _getListApiByType(type) {
        switch (type) {
            case 'person':
                return 'student/list'
            case 'project':
                return 'origanziation/list'
            default:
                throw Error(`unknown type: ${type}`)
        }
    }

    static _getVoteApiByType(type) {
        switch (type) {
            case 'person':
                return {
                    url: 'coin/student',
                    idType: 'sid'
                }
            case 'project':
                return {
                    url: 'coin/origanziation',
                    idType: 'oid'
                }
            default:
                throw Error(`unknown type: ${type}`)
        }
    }
}

export {Vote}
